/*
 * nanduData.java
 *
 * Created on February 6, 2007, 8:54 PM
 *
 *  Contains list of valid nandus for both judge and head judge to access
 *  If you change any content in this file please make the exact change in 
 *  the HeadJudges or Judge nandudata.java file too.
 *
 * @author Sujal Bista
 */
import java.util.Vector;
import java.io.*;
public class nanduData 
{
    public class nandu
    {
        public int id;              //program generated ID
        public String type;         //type like jumps, connection movements ...
        public String description;  //explanation
        public String code;         //code specified in the book
        public String points;       //total scorable points
        public String validEvent;   //event the nandu is valid to CQ, NQ, TJ ..
        
        nandu(int id,String type,String description,String code,String points,String validEvent)
        {
            this.id=id;
            this.type=type;
            this.description=description;
            this.code=code;
            this.points=points;
            this.validEvent=validEvent;
        }
        
        public String toString()
        {
            return "Type : "+type+"\tCode : "+code+"\tPoints : "+points+"\nDetail : "+description;
        }
        
        public int getTotalMovements()
        {
            
            String tempNandu=new String(points) ;
            String value="";
            int count=0;
            while(tempNandu.length()!=0)
            {

                if(tempNandu.indexOf(",")==-1)
                {
                    value=tempNandu;
                    tempNandu="";
                }
                else
                {
                   value=tempNandu.substring(0, tempNandu.indexOf(","));
                   tempNandu=tempNandu.substring(tempNandu.indexOf(",")+1);
                }
                count++;
            }
            return count;
        }
        
        public double getPoint(int index)
        {
            
            String tempNandu=new String(points) ;
            String value="";
            int count=0;
            while(tempNandu.length()!=0)
            {

                if(tempNandu.indexOf(",")==-1)
                {
                    value=tempNandu;
                    tempNandu="";
                }
                else
                {
                   value=tempNandu.substring(0, tempNandu.indexOf(","));
                   tempNandu=tempNandu.substring(tempNandu.indexOf(",")+1);
                }
                if(count==index)
                    break;
                count++;
            }
            return Double.valueOf(value);
        }
        
        public String getCode(int index)
        {
            
            String tempNandu=new String(code) ;
            String value="";
            int count=0;
            while(tempNandu.length()!=0)
            {

                if(tempNandu.indexOf(",")==-1)
                {
                    value=tempNandu;
                    tempNandu="";
                }
                else
                {
                   value=tempNandu.substring(0, tempNandu.indexOf(","));
                   tempNandu=tempNandu.substring(tempNandu.indexOf(",")+1);
                }
                if(count==index)
                    break;
                count++;
            }
            return value;
        }
        
    }
    
    public nandu nanduList[];   //list of valid nandu for the tournament
    public int totalNandu;
    
    
    /** Creates a new instance of nanduData */
    public nanduData() 
    {
         //list of scoring system
        Vector tempNanduList = new Vector();
        String fileName="../data/nandu.txt";
        String fileContent;
        int index,count=0;
        try
        {
            BufferedReader inputScript=new BufferedReader(new FileReader(fileName));

            do
            {
                fileContent=inputScript.readLine();
                if(fileContent==null)
                    break;

                index=fileContent.indexOf("//");
                
                //skip if it is comment
                if(index>=0)
                    continue;
                
                index=fileContent.indexOf(":");
                
                //check if its nandu data
                if(index<0)
                    continue;             
                
                
                String type=fileContent.substring(0, index);
                
                fileContent=fileContent.substring(index+1);
                index=fileContent.indexOf(":");
                String description=fileContent.substring(0, index);
                
                fileContent=fileContent.substring(index+1);
                index=fileContent.indexOf(":");
                String code=fileContent.substring(0, index);
                
                fileContent=fileContent.substring(index+1);
                index=fileContent.indexOf(":");
                String points=fileContent.substring(0, index);
                                
                fileContent=fileContent.substring(index+1);
                String validEvents=fileContent;
                
                nandu data=new nandu(count,type.trim(),description.trim(),code.trim(),points.trim(),validEvents.trim());
                
                //add to the list
                tempNanduList.addElement(data);
                
                count++;
            }while(true);

            inputScript.close();
        }catch(FileNotFoundException e)
        {
              System.err.println("File \""+fileName+"\" not found. ");
              System.exit(1);
        }
        catch(IOException e)
        {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        
        //get total nandu count
        totalNandu=tempNanduList.size();
        
        //allocate space
        nanduList=new nandu[totalNandu];
        
        //add nandu to the list
        for(int i=0;i<totalNandu;i++)
        {
            nanduList[i]=(nandu)tempNanduList.elementAt(i);
        }
        
    
    
    }
    
    public nandu find(String code)
    {
        for(int i=0;i<totalNandu;i++)
        {
            String tempCode=nanduList[i].code.replace(",","");
            if(tempCode.equals(code))
                return nanduList[i];
        }
        return null;
    }
    
    public nandu find(String code,String eventName)
    {
         for(int i=0;i<totalNandu;i++)
        {
            String temp=nanduList[i].code;

            while(temp.indexOf(',')>=0)
                temp=temp.substring(0,temp.indexOf(','))+temp.substring(temp.indexOf(',')+1);
            
            if(temp.equals(code) && nanduList[i].validEvent.indexOf(eventName)!=-1)
                return nanduList[i];
        }
        return null;
    }
    
    //given the nandu string returns the nandu code
    public String getNandu(int i,String nanduString)
    {
        String tempNandu=new String(nanduString) ;
        String value="";
        int count=0;
        while(tempNandu.length()!=0)
        {
            
            if(tempNandu.indexOf(",")==-1)
            {
                value=tempNandu;
                tempNandu="";
            }
            else
            {
               value=tempNandu.substring(0, tempNandu.indexOf(","));
               tempNandu=tempNandu.substring(tempNandu.indexOf(",")+1);
            }
            if(count==i)
                break;
            count++;
        }
        return value;
    }
    
    //returns total nandu count given nandu string
    public int getNanduCount(String nanduString)
    {
        String tempNandu=new String(nanduString) ;
        int count=0;
        while(tempNandu.length()!=0)
        {
            String value;
            if(tempNandu.indexOf(",")==-1)
            {
                value=tempNandu;
                tempNandu="";
            }
            else
            {
               value=tempNandu.substring(0, tempNandu.indexOf(","));
               tempNandu=tempNandu.substring(tempNandu.indexOf(",")+1);
            }
            count++;
        }
        return count;
    }
    
    //includes sub parts of Nandu given nandu string
    public int getMovementsCount(String nanduString)
    {
        int count=0;
        for(int i=getNanduCount(nanduString)-1;i>=0;i--)
        {
            nanduData.nandu nd=find(getNandu(i,nanduString));
            String abc=getNandu(i,nanduString);
            if(nd==null)
                nd=null;
            count+=nd.getTotalMovements();
        }
        return count;
    } 
}
